package com.transaction.manage.localMapper;

import com.transaction.manage.model.dto.MarketDate;
import com.transaction.manage.model.vo.MarketDateVo;
import com.transaction.manage.myMapper.MyMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MarketDateMapper extends MyMapper<MarketDate> {

    void insertMarketDates(MarketDateVo marketDateVo);

    @Delete("delete from market_date where market_code = #{marketCode} and year = #{year} ")
    void deleteMarketDate(@Param("marketCode") String marketCode,
                          @Param("year") String year);


    List<MarketDate> selectMarketDates(MarketDate marketDate);

    @Select("SELECT count(*) FROM market_date m WHERE m.date = #{handleDate} " +
            "AND EXISTS ( SELECT 1 FROM channel_info c WHERE c.channel_code = #{channelCode} " +
            "AND c.valid_flag != '00' AND c.check_flag = '01' " +
            "AND c.market_code = m.market_code )")
    int checkHandleDateIsWorkDate(@Param("channelCode") String channelCode,
                                  @Param("handleDate") String handleDate);


    @Select("select DISTINCT m.week from market_date m where m.date = #{handleDate} ")
    Integer selectHandleDateWeek(@Param("handleDate") String handleDate);

    @Select("SELECT date FROM market_date t " +
            "WHERE t.market_code = ( SELECT market_code FROM channel_info WHERE channel_code = #{channelCode} ) " +
            "AND t.date > #{handleDate} ORDER BY t.date LIMIT #{n}, 1")
    String getNextNDay(@Param("channelCode") String channelCode,
                       @Param("handleDate") String handleDate,
                       @Param("n") int n);

    @Select("SELECT date FROM market_date t " +
            "WHERE t.market_code = ( SELECT market_code FROM channel_info WHERE channel_code = #{channelCode} ) " +
            "AND t.date < #{handleDate} ORDER BY t.date DESC LIMIT #{n}, 1")
    String getLastNDay(@Param("channelCode") String channelCode,
                       @Param("handleDate") String handleDate,
                       @Param("n") int n);
}